package com.wc.codeforces.数学.约数.Digits;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/1/4 10:58
 * @description
 * https://codeforces.com/contest/2043/problem/B
 */
public class Main {
    /**
     * 思路：
     * 1 一定满足
     * 3 7 9
     * 查最少需要 num 个 d 连接才能满足 i 的倍数, 查找 num的质因数个数,
     * 查看 n! 当前质因数个数是否能够, 如果不够说明无法达到, 反之满足
     * 5 d = 5才行
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int n, d;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            d = sc.nextInt();
            for (int i = 1; i <= 9; i += 2) {
                long x = d;
                int num = 1;
                // 计算最少需要 num 个 d 连接才能满足 i 的倍数
                while (x % i != 0) {
                    x = x * 10 + d;
                    num++;
                }
                boolean success = false;
                // 5 是特殊情况
                if (i == 5 && d != 5) continue;

                if (i == 1) success = true;
                else {
                    boolean flg = true;
                    // 查找 num的质因数个数 s
                    for (int k = 2; k * k <= num; k++) {
                        if (num % k == 0) {
                            int s = 0;
                            while (num % k == 0) {
                                num /= k;
                                s ++;
                            }
                            // n! 关于 k的质因数个数 s
                            int y = n, t = 0;
                            while (y > 0) {
                                t += y / k;
                                y /= k;
                            }
                            // 是否满足条件
                            if (t < s) {
                                flg = false;
                                break;
                            }
                        }
                    }
                    // 不要忘记最后一个质因数
                    if (num > 1) {
                        int y = n, t = 0, s = 1, k = num;
                        while (y > 0) {
                            t += y / k;
                            y /= k;
                        }
                        if (t < s) flg = false;
                    }

                    success = flg;
                }
                if (success) out.print(i + " ");
            }
            out.println();
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

